package com.tusuo.stds.ui.main;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.constraint.ConstraintLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.tusuo.stds.R;
import com.tusuo.stds.rest.SceneList;
import com.tusuo.stds.util.GlideRoundTransform;
import com.tusuo.stds.util.Utils;

import java.util.List;

/**
 * @author J.Huang
 * @version 1.0
 *
 */
public class SceneListAdapter extends BaseAdapter {

    private List<SceneList.Scene> dataList;
    private LayoutInflater layoutInflater;
    private Context context;

    public SceneListAdapter(Context context, List<SceneList.Scene> dataList) {
        this.context = context;
        this.dataList = dataList;
        this.layoutInflater = LayoutInflater.from(context);
    }

    private final class Component {
        LinearLayout layout;
        TextView tvTitle;
        TextView tvSubtitle;
        TextView tvNumber;
        ImageView ivLock;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public SceneList.Scene getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Component component;
        View view;
        if (convertView == null) {
            component = new Component();
            // 获得组件，实例化组件
            view = layoutInflater.inflate(R.layout.scene_list_item, null);

            component.layout = (LinearLayout) view.findViewById(R.id.challenge_list_item_layout);
            component.tvTitle = (TextView) view.findViewById(R.id.challenge_list_item_tv_title);
            component.tvSubtitle = (TextView) view.findViewById(R.id.challenge_list_item_tv_subtitle);
            component.tvNumber = (TextView) view.findViewById(R.id.challenge_list_item_tv_number);
            component.ivLock = (ImageView) view.findViewById(R.id.challenge_list_item_iv_lock);

            view.setTag(component);
        } else {
            view = convertView;
            component = (Component) view.getTag();
        }

        SceneList.Scene scene = dataList.get(position);


        Glide
                .with(context)
                .load(Utils.getCorrectImageUrl(scene.ImageUrl))
                .asBitmap()
                .transform(new GlideRoundTransform(context, 10))
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        Drawable drawable = new BitmapDrawable(context.getResources(), resource);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            component.layout.setBackground(drawable);
                        }
                    }
                });

        component.tvTitle.setText(scene.Title);
        component.tvSubtitle.setText(scene.Description);
        component.tvNumber.setText(String.valueOf(scene.JoinCount));

        if (scene.LockStatus == 0) {
            component.ivLock.setVisibility(View.VISIBLE);
        } else {
            component.ivLock.setVisibility(View.INVISIBLE);
        }

        return view;
    }
}
